package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageOtherMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageOtherOverviewActivity;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageOtherOverviewBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * 作者：wsy on 2017/12/7 15:14
 * <p>
 * 邮箱：445979770@qq.com
 */

public class OtherOutStorageMaterielOverviewAdapter extends BaseAdapter {
    private Context mContext;
    private List<OutStorageOtherOverviewBean.BodyBean> mAllocatingOutStorageItemList;
    private LayoutInflater inflater;
    private OutStorageOtherOverviewActivity.OutStorageOtherOverviewListener mOutStorageAllocatingListener;
    private String mMaterielCode;

    public OtherOutStorageMaterielOverviewAdapter(Context context, List<OutStorageOtherOverviewBean.BodyBean> outStoragePAllocatingItemList, String materielCode, OutStorageOtherOverviewActivity.OutStorageOtherOverviewListener outStoragAllocatingOverviewListener) {
        mContext = context;
        mMaterielCode = materielCode;
        mAllocatingOutStorageItemList = outStoragePAllocatingItemList;
        mOutStorageAllocatingListener = outStoragAllocatingOverviewListener;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return mAllocatingOutStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mAllocatingOutStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_other_select_supplier_list_first_item, null);
            EditText materielCode = (EditText) convertView.findViewById(R.id.supplierCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            materielCode.setText(mMaterielCode);
            materielCode.setHint(mContext.getResources().getString(R.string.activity_inventory_select_materiel_adapter_please_input_material_code));
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mOutStorageAllocatingListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mOutStorageAllocatingListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            OutStorageOtherOverviewBean.BodyBean PickingOutstorageItem = mAllocatingOutStorageItemList.get(position - 1);
            OtherOutStorageMaterielOverviewAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_allocating_item_list, null);
                viewHolder = new OtherOutStorageMaterielOverviewAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (OtherOutStorageMaterielOverviewAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, PickingOutstorageItem);
        }


        return convertView;
    }

    private void initView(OtherOutStorageMaterielOverviewAdapter.ViewHolder viewHolder, View view) {
        viewHolder.serialNumberTextView = (TextView) view.findViewById(R.id.serialNumberTextView);
        viewHolder.serialNumber = (TextView) view.findViewById(R.id.serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.canSendNumber = (TextView) view.findViewById(R.id.canSendNumber);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, OtherOutStorageMaterielOverviewAdapter.ViewHolder viewHolder, View view, OutStorageOtherOverviewBean.BodyBean pallocatingOutStorageItem) {
//        viewHolder.serialNumber.setText(pallocatingOutStorageItem.getSerial());
        viewHolder.serialNumberTextView.setText("");
        viewHolder.materielCode.setText(pallocatingOutStorageItem.getMat_code());
        viewHolder.materielDescribe.setText(pallocatingOutStorageItem.getMat_name());
        viewHolder.canSendNumber.setText("可发" + pallocatingOutStorageItem.getQty() + pallocatingOutStorageItem.getUnit_name());
        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(pallocatingOutStorageItem.getActualSendNumber()) ? mContext.getResources().getString(R.string.activity_out_storage_sale_overview_real_hair)
                : (mContext.getResources().getString(R.string.activity_out_storage_sale_overview_fill_in_the_number) + pallocatingOutStorageItem.getActualSendNumber() + pallocatingOutStorageItem.getUnit_name()));
        if (TextUtils.isEmpty(pallocatingOutStorageItem.getActualSendNumber())) {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        }
//        view.setOnClickListener(new NoDoubleClickListener() {
//            @Override
//            public void onNoDoubleClick(View v) {
//                Intent intent = new Intent(context, OutStorageAllocatingDetailActivity.class);
//                String serial = viewHolder.serialNumber.getText().toString();
//                intent.putExtra(OutStorageExtra.SERIAL, serial);
//                intent.putExtra(OutStorageExtra.SEND_FACTORY_CODE, mSendFactoryCode);
//                intent.putExtra(OutStorageExtra.SEND_LOCATION_CODE, mSendLoctionCode);
//                context.startActivity(intent);
//            }
//        });
        viewHolder.materielDescribe.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
//                if (mOutStorageAllocatingListener.onCheckData()) {
                Intent intent = new Intent(context, OutStorageOtherMaterielDetailActivity.class);
                intent.putExtra(InStorageExtra.MATERIEL_CODE, pallocatingOutStorageItem.getMat_code());
                context.startActivity(intent);
//                }
            }
        });
        view.setOnLongClickListener((View v) -> {
            mOutStorageAllocatingListener.deleteMaterielItem(pallocatingOutStorageItem.getMat_code());
            return true;
        });
    }

    private class ViewHolder {
        private TextView serialNumberTextView, serialNumber, materielCode, materielDescribe, canSendNumber, outStorageNumber;

        private PercentRelativeLayout headLayout;
    }
}
